package com.cms.setting.nav.service.impl;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cms.framework.utils.BeanUtilEx;
import com.cms.framework.utils.page.PageView;
import com.cms.setting.nav.dao.NavCatDAO;
import com.cms.setting.nav.pojo.NavCat;
import com.cms.setting.nav.pojo.dto.NavCatDTO;
import com.cms.setting.nav.service.NavCatService;
@Service
public class NavCatServiceImpl implements NavCatService {
	@Autowired
	NavCatDAO navCatDAO;
	@Override
	public boolean saveNavCat(NavCat navCat) {
		if(navCat.getId()==null){
			return navCatDAO.insert(navCat)>0;
		}else{
			return navCatDAO.updateByPrimaryKey(navCat)>0;
		}
	}

	@Override
	public boolean deleteByCatId(Integer catId) {
		return navCatDAO.deleteByPrimaryKey(catId)>0;
	}

	@Override
	public NavCat findByCatId(Integer catId) {
		return navCatDAO.selectByPrimaryKey(catId);
	}

	@Override
	public List<NavCat> findList() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public PageView<NavCatDTO> findListByPage(PageView<NavCatDTO> pageView,
			NavCatDTO navCatDTOParams) {
		List<NavCat> list=navCatDAO.selectByPage(pageView, navCatDTOParams);
		List<NavCatDTO> dtoList=new ArrayList<NavCatDTO>();
		for(NavCat navCat:list){
			NavCatDTO navCatDTO=new NavCatDTO();
			try {
				BeanUtilEx.copyProperties(navCatDTO, navCat);
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				e.printStackTrace();
			}
			dtoList.add(navCatDTO);
		}
		pageView.setRecords(dtoList);
		return pageView;
	}

}
